#include "BieuThucDon.h"


BieuThucDon::BieuThucDon()
{
	heSo = 0;
	soMu = 1;
}

BieuThucDon::BieuThucDon(double heSo, double soMu)
{
	this->heSo = heSo;
	this->soMu = soMu;
}

BieuThuc* BieuThucDon::taoBieuThucDon(double heSo, double soMu)
{
	if( heSo == 0 || soMu == 0)
		return BieuThucHang::taoBieuThucHang(heSo);		// Thinh cau c: Khai bao BieuThucHang 								
																							// o cau b se duoc thay vao sau 
	else
		return (new BieuThucDon(heSo, soMu));
}

double BieuThucDon::tinhGiaTri(double x)
{
	double kq = 1;
	for(int i=0; i<soMu; i++)
	{
		kq = kq*x;
	}
	return heSo*kq;
}

//BieuThuc* BieuThucDon::tinhDaoHam(){
//	double heSoMoi = heSo*soMu;
//	double soMuMoi = soMu - 1;
//
//	BieuThuc* btkq = new BieuThucDon(heSoMoi, soMuMoi);
//	return btkq;
//}

BieuThuc* BieuThucDon::timDaoHam()
{
	double heSoMoi = heSo*soMu;
	double soMuMoi = soMu - 1;

	BieuThuc* btkq = BieuThucDon::taoBieuThucDon(heSoMoi, soMuMoi);
	return btkq;
}

void BieuThucDon::daoDauBieuThuc()
{
	this->heSo = heSo * (-1);
}

string BieuThucDon::xuatChuoi()
{
	std::ostringstream sstr;
	std::ostringstream sstr1;

	if(this->heSo == 0)
		return "";
	else
	{
		if(this->soMu == 0)
		{
			sstr << heSo;
			return sstr.str();
		}
		else
		{
			if(this->soMu == 1)
			{
				sstr << heSo;
				return sstr.str() + "x";
			}
			else
			{
				sstr << heSo;
				sstr1 << soMu;
				return sstr.str() + "x^" + sstr1.str();
			}
		}
	}
}
